//
//  HUD.swift
//  ZZUI
//
//  Created by 张文晏 on 2018/10/26.
//  Copyright © 2018 张文晏. All rights reserved.
//

import Foundation
import MBProgressHUD

open class HUD {
    private enum HUDType {
        /// 加载
        case loading(message: String)
        /// 成功提示
        case success(message: String)
        /// 失败提示
        case failure(message: String)
        /// 警告信息
        case warning(message: String)
        /// 中间信息提示
        case centerMessage(message: String)
        /// 底部信息提示
        case botomMessage(message: String)
    }
    
    public enum Position {
        case center, bottom
    }
    
    private init() {}
    
    /// 加载提示
    public static func showLoading(_ message: String = "正在加载", in view: UIView = UIApplication.shared.keyWindow ?? UIView()) {
        HUD.show(type: .loading(message: message), in: view)
    }
    
    /// 成功提示
    ///
    /// - Parameters:
    ///   - message: 提示信息
    public static func showSuccess(_ message: String = "操作成功", in view: UIView = UIApplication.shared.keyWindow ?? UIView()) {
        HUD.show(type: .success(message: message), in: view)
    }
    
    /// 失败提示
    ///
    /// - Parameters:
    ///   - message: 提示信息
    public static func showFailure(_ message: String = "操作失败", in view: UIView = UIApplication.shared.keyWindow ?? UIView()) {
        HUD.show(type: .failure(message: message), in: view)
    }
    
    /// 警告信息
    ///
    /// - Parameters:
    ///   - message: 提示信息
    public static func showWarning(_ message: String, in view: UIView = UIApplication.shared.keyWindow ?? UIView()) {
        HUD.show(type: .warning(message: message), in: view)
    }
    
    /// 信息提示
    ///
    /// - Parameters:
    ///   - message: 提示信息
    public static func showMessage(_ message: String, in view: UIView = UIApplication.shared.keyWindow ?? UIView(), position: Position = .bottom) {
        switch position {
        case .center:
            HUD.show(type: .centerMessage(message: message), in: view)
        case .bottom:
            HUD.show(type: .botomMessage(message: message), in: view)
        }
    }
    
    /// 隐藏 HUD
    public static func hide(in view: UIView = UIApplication.shared.keyWindow ?? UIView()) {
        for sub in view.subviews where sub is MBProgressHUD {
            guard let mbv = sub as? MBProgressHUD else { break }
            mbv.hide(animated: false)
        }
    }
    
    private static func show(type: HUDType, in view: UIView) {
        view.endEditing(true)
        let hud = MBProgressHUD.showAdded(to: view, animated: true)
        hud.mode = .customView
        switch type {
        case .loading(let message):
            hud.isUserInteractionEnabled = true
            hud.offset = .zero
            hud.isSquare = true
            let imv = UIImageView(image: NCUIBundle.image("ui_hud_loading"))
            let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
            rotationAnimation.toValue = CGFloat.pi * 2
            rotationAnimation.duration = 1.5
            rotationAnimation.repeatCount = 10000
            imv.layer.add(rotationAnimation, forKey: "rotationAnimation")
            hud.customView = imv
            hud.label.text = message
        case .success(let message):
            hud.isUserInteractionEnabled = false
            hud.offset = .zero
            hud.isSquare = true
            hud.customView = UIImageView(image: NCUIBundle.image("ui_hud_success"))
            hud.label.text = message
            hud.hide(animated: true, afterDelay: 2)
        case .failure(let message):
            hud.isUserInteractionEnabled = false
            hud.offset = .zero
            hud.isSquare = true
            hud.customView = UIImageView(image: NCUIBundle.image("ui_hud_failure"))
            hud.label.text = message
            hud.hide(animated: true, afterDelay: 2)
        case .warning(let message):
            hud.isUserInteractionEnabled = false
            hud.offset = .zero
            hud.isSquare = true
            hud.customView = UIImageView(image: NCUIBundle.image("ui_hud_warning"))
            hud.label.text = message
            hud.hide(animated: true, afterDelay: 2)
        case .centerMessage(let message):
            hud.isUserInteractionEnabled = false
            hud.offset = .zero
            hud.isSquare = false
            hud.customView = nil
            hud.label.text = message
            hud.hide(animated: true, afterDelay: 2)
        case .botomMessage(let message):
            hud.isUserInteractionEnabled = false
            hud.offset = CGPoint(x: 0, y: MBProgressMaxOffset)
            hud.isSquare = false
            hud.customView = nil
            hud.label.text = message
            hud.hide(animated: true, afterDelay: 2)
        }
        hud.margin = 12
        hud.bezelView.color = .black
        hud.label.numberOfLines = 0
        hud.label.font = .systemFont(ofSize: 14)
        hud.label.textColor = .white
    }
}
